#include "collisionlayer.h"
#include "stringproperty.h"
#include "remarkactorbase.h"
#include "collisionPlaneActor.h"
#include "transformable.h"

namespace VrDataCore
{
	CollisionLayer::CollisionLayer():_disable(true)
	{
		setClassName(CLASS_COLLISION_LAYER);
	}

	CollisionLayer::~CollisionLayer()
	{}

	void CollisionLayer::addChild(CoreBase::ActorBase* mactor)
	{
		if(mactor->isInstanceOf(CLASS_COLLISION_PLANE))
		{
			getDrawable()->addChild(mactor->getDrawable());
			if(!_allChild.contains(mactor->getId()))
				_allChild.insert(mactor->getId(),mactor);
			_collisionPlaneActor.push_back(osg::ref_ptr<CoreBase::ActorBase>(mactor));
			if(!_disable)
				dynamic_cast<CollisionPlaneActor*>(mactor)->setActive(true);
			else
				dynamic_cast<CollisionPlaneActor*>(mactor)->setActive(false);
		}
		else
		{
			_collisionDeviceActor.push_back(mactor);
			if(!_disable)
				dynamic_cast<CoreBase::Transformable*>(mactor->getDrawable())->renderCollisionGeometry();
			else
				dynamic_cast<CoreBase::Transformable*>(mactor->getDrawable())->renderCollisionGeometry(false);
		}
	}

	void CollisionLayer::removeChild(CoreBase::ActorBase* mactor)
	{
		if(mactor==NULL || mactor->getDrawable()==NULL)
			return;
		if(mactor->isInstanceOf(CLASS_COLLISION_PLANE))
		{
			if(mactor->isInstanceOf(CLASS_DEVICE_GROUP))
			dynamic_cast<DeviceGroup*>(mactor)->clearAllChildren();
			_allChild.remove(mactor->getId());
			getDrawable()->removeChild(mactor->getDrawable());
			_collisionPlaneActor.remove(_collisionPlaneActor.indexOf(mactor));
		}
		else
			_collisionDeviceActor.remove(_collisionDeviceActor.indexOf(mactor));
	}

	void CollisionLayer::buildPropertyMap()
	{
		addProperty(new StringProperty<CollisionLayer>(XML_ELEMENT_DATATYPE_STRING,
			"Vertex Data","",this,&CollisionLayer::setVertexData,
			&CollisionLayer::getVertexData,"",""));
		addProperty(new StringProperty<DrawableActor>(XML_ELEMENT_DATATYPE_STRING,
			"Parent ID","",this,&DrawableActor::setParentID,
			&DrawableActor::getParentID,"",""));
	}

	void CollisionLayer::setVertexData(QString vertexstring)
	{
		_vertexString = vertexstring;
		QStringList vertexlist = _vertexString.split("|",QString::SkipEmptyParts);
		for(int i=0;i<vertexlist.size();++i)
		{
			QStringList everyplanevertex = vertexlist.at(i).split(",",QString::SkipEmptyParts);
			if(everyplanevertex.size() == 2)
			{
				QStringList startvertex = everyplanevertex.at(0).split(" ",QString::SkipEmptyParts);
				QStringList endvertex = everyplanevertex.at(1).split(" ",QString::SkipEmptyParts);
				if(startvertex.size() == endvertex.size() && endvertex.size() == 3)
				{
					osg::Vec3 start(startvertex.at(0).toFloat(),startvertex.at(1).toFloat(),startvertex.at(2).toFloat());
					osg::Vec3 end(endvertex.at(0).toFloat(),endvertex.at(1).toFloat(),endvertex.at(2).toFloat());
					osg::ref_ptr<CollisionPlaneActor> actor = new CollisionPlaneActor;
					actor->init();
					actor->setControlVertex(start,end);
					addChild(actor);
				}
			}
		}
	}

	QString CollisionLayer::getVertexData()
	{
		_vertexString = "";
		for(int i=0;i<_collisionPlaneActor.size();++i)
		{
			osg::Vec3 start,end;
			dynamic_cast<CollisionPlaneActor*>(_collisionPlaneActor.at(i).get())->getControlVertex(start,end);
			_vertexString = _vertexString+"|";
			_vertexString = _vertexString + QString::number(start.x()) + " "+QString::number(start.y()) + " "+QString::number(start.z());
			_vertexString = _vertexString+",";
			_vertexString = _vertexString + QString::number(end.x()) + " "+QString::number(end.y()) + " "+QString::number(end.z());
		}
		return _vertexString;
	}

	QVector<osg::ref_ptr<CoreBase::ActorBase>> CollisionLayer::getCollisionPlaneActorVector()
	{
		return _collisionPlaneActor;
	}

	void CollisionLayer::setActive(QString active)
	{
		if(active == "false")
			hideCollisionShape();
		else
			showCollisionShape();
	}

	void CollisionLayer::showCollisionShape()
	{
		if(!_disable)
			return;
		for(int i=0;i<_collisionPlaneActor.size();++i)
		{
			dynamic_cast<CollisionPlaneActor*>(_collisionPlaneActor.at(i).get())->setActive(true);
		}
		for(int i=0;i<_collisionDeviceActor.size();++i)
		{
			dynamic_cast<CoreBase::Transformable*>(_collisionDeviceActor.at(i)->getDrawable())->renderCollisionGeometry();
		}
		_disable = false;
	}

	void CollisionLayer::hideCollisionShape()
	{
		if(_disable)
			return;
		for(int i=0;i<_collisionPlaneActor.size();++i)
		{
			dynamic_cast<CollisionPlaneActor*>(_collisionPlaneActor.at(i).get())->setActive(false);
		}
		for(int i=0;i<_collisionDeviceActor.size();++i)
		{
			dynamic_cast<CoreBase::Transformable*>(_collisionDeviceActor.at(i)->getDrawable())->renderCollisionGeometry(false);
		}
		_disable = true;
	}

	QString CollisionLayer::getActive()
	{
		if(_disable)
			return "false";
		else
			return "true";
	}

	void CollisionLayer::startUpdateOde(CoreBase::Viewer* viewer,CoreBase::Scene* scene)
	{}

	void CollisionLayer::stopUpdateOde()
	{}
}